
#include <QtGui>
#include <QSpinBox>
#include <QComboBox>
#include <QLineEdit>
#include "QtStra3_InstTableDelegate.h"
#include "nonqtmain.h"

//! [0]
QtStra3InstTableDelegate::QtStra3InstTableDelegate(QObject *parent)
	: QItemDelegate(parent)
{
}
//! [0]

//! [1]
QWidget *QtStra3InstTableDelegate::createEditor(QWidget *parent,
	const QStyleOptionViewItem & option ,
	const QModelIndex & index ) const
{
	if (index.column() == 1)
	{
		QSpinBox *editor = new QSpinBox(parent);
		editor->setMinimum(0);
		editor->setMaximum(100);

		return editor;
	}
	else if (index.column() == 2)
	{
		QComboBox *editor = new QComboBox(parent);
	
		editor->addItem(StrategyUser4_CInstMap_NameSpace::CInstMap::OPEN.c_str());
		editor->addItem(StrategyUser4_CInstMap_NameSpace::CInstMap::CLOSE.c_str());
		
		return editor;
	}
	else if (index.column() == 3)
	{
		QComboBox *editor = new QComboBox(parent);

		editor->addItem(StrategyUser4_CInstMap_NameSpace::CInstMap::BUY.c_str());
		editor->addItem(StrategyUser4_CInstMap_NameSpace::CInstMap::SELL.c_str());

		return editor;
	}
	else if (index.column() == 4)
	{
		QLineEdit *editor = new QLineEdit(parent);

		editor->setValidator(new QDoubleValidator(0,
			999999.0, 2, editor));

		return editor;
	}
	else 
	{
		//return QItemDelegate::createEditor(parent, option, index);
		return NULL;
	}
}
//! [1]

//! [2]
void QtStra3InstTableDelegate::setEditorData(QWidget *editor,
	const QModelIndex &index) const
{
	if (index.column() == 1)
	{
		int value = index.model()->data(index, Qt::EditRole).toInt();

		QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
		spinBox->setValue(value);
	}
	else if (index.column() == 2 || index.column() == 3)
	{
		QString value = index.model()->data(index, Qt::EditRole).toString();

		QComboBox *cBox = static_cast<QComboBox*>(editor);
		cBox->setCurrentIndex(cBox->findText(value));
	}
	else if (index.column() == 4)
	{

		QString value = index.model()->data(index, Qt::EditRole).toString();

		QLineEdit *l = static_cast<QLineEdit*>(editor);

		l->setText(value);

		

	}

	//else {
	//	QItemDelegate::setEditorData(editor, index);
	//	//return NULL;
	//}
}
//! [2]

//! [3]
void QtStra3InstTableDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
	const QModelIndex &index) const
{
	if (index.column() == 1)
	{
		QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
		spinBox->interpretText();
		int value = spinBox->value();

		model->setData(index, value, Qt::EditRole);
	}
	else if (index.column() == 2 || index.column() == 3)
	{
		QComboBox *cBox = static_cast<QComboBox*>(editor);

		int i = cBox->currentIndex();

		QString value = cBox->currentText();

		model->setData(index, value, Qt::EditRole);
	}
	else if (index.column() == 4)
	{
		QLineEdit *l = static_cast<QLineEdit*>(editor);


		double value = l->text().toDouble();

		model->setData(index, value, Qt::EditRole);
	}
}
//! [3]


